package com.libre.event;

import com.libre.constant.SysConstant;
import com.libre.view.file.ShowView;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * NOTE 文件拖到控件上方，鼠标松开事件
 */
public class DragDroppedEvent implements EventHandler<DragEvent> {

    private TableView<Map<String, Object>> table = new TableView<>();
    private List<Map<String, Object>> list = new ArrayList<>();

    public DragDroppedEvent(TableView<Map<String, Object>> table, List<Map<String, Object>> list) {
        this.table = table;
        this.list = list;
    }

    public void handle(DragEvent event) {
        Dragboard dragboard = event.getDragboard();
        if (dragboard.hasFiles()) {
            try {
                List<File> files = dragboard.getFiles();
                add(table, list, files);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 判断list中数据是否重复
    private static Boolean isRepeat(List<Map<String, Object>> list, String path) {
        Boolean r = false;
        for (Map<String, Object> map : list) {
            if (path.equals(map.get("filePath") + "")) {
                r = true;
                break;
            }
        }
        return r;
    }

    //向表格中添加数据
    public static void add(TableView<Map<String, Object>> table, List<Map<String, Object>> list, List<File> files) {
        if (table != null) {
            int i = 0;//拖拽的文件中重复的文件
            for (File file : files) {
                if (isRepeat(list, file.getAbsolutePath())) {
                    i++;
                    continue;//重复的不在添加
                }
                Map<String, Object> map = new HashMap<>();
                map.put("filePath", file.getAbsolutePath());

                GridPane pane = new GridPane();
                pane.setAlignment(Pos.CENTER);
                pane.setHgap(5);
                pane.setVgap(5);

                Button view = new Button("查看"), del = new Button("删除");
                //-查看-
                Image viewImage = new Image("./static/icon/file.png");
                ImageView viewImageView = new ImageView(viewImage);
                viewImageView.setFitWidth(SysConstant.BUTTON_ICON_WIDTH);
                viewImageView.setFitHeight(SysConstant.BUTTON_ICON_HEIGHT);
                view.setGraphic(viewImageView);
                view.setOnAction(event -> new ShowView());


                //-删除-
                Image delImage = new Image("./static/icon/ashbin.png");
                ImageView delImageView = new ImageView(delImage);
                delImageView.setFitWidth(SysConstant.BUTTON_ICON_WIDTH);
                delImageView.setFitHeight(SysConstant.BUTTON_ICON_HEIGHT);
                del.setGraphic(delImageView);

                pane.add(view, 0, 1);
                pane.add(del, 1, 1);
                map.put("operate", pane);
                list.add(map);
            }
            if (i > 0) {
                Stage stage = new Stage();
                DialogPane dialogPane = new DialogPane();
                dialogPane.setContent(new Label(String.format("上传的文件中有【%s】份已经重复，系统已自动为您删除", i)));
                Scene scene = new Scene(dialogPane);
                stage.setScene(scene);
                stage.show();
            }
            ObservableList<Map<String, Object>> data = FXCollections.observableArrayList(list);
            table.setItems(data); // 设置数据源
        }
    }

}
